"""Copyright 2011 Thomas Anesta, Jonathan Brenner, Tyler Moylat, Rosa Tung, Sean Kim"""

"""

This file is part of Bubbles

Bubbles is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Bubbles is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

"""

import sys, pygame
import BubblesConstants

SHOT_S_WIDTH = 6
SHOT_S_HEIGHT = 6
SHOT_M_WIDTH = 10
SHOT_M_HEIGHT = 10
SHOT_L_WIDTH = 16
SHOT_L_HEIGHT = 16
SHOT_E_WIDTH = 22
SHOT_E_HEIGHT = 22

HOMING_FRAMES_LIST = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]
HOMING_FRAMES = 10
HOMING_FRAMES_DELAY = 2
HOMING_TIMER = 240

class Shot(object):
    def __init__(self, x, y, xvel, yvel, alignment, player):
        """creates the shot"""
        self.type = BubblesConstants.SHOT_TYPE
        self.image_s = pygame.image.load("gfx/S_Bubble_Shot.png").convert_alpha()
        self.image_m = pygame.image.load("gfx/M_Bubble_Shot.png").convert_alpha()
        self.image_l = pygame.image.load("gfx/L_Bubble_Shot.png").convert_alpha()
        self.player = player
        self.player_size = player.size
        self.image = self.image_m # default
        self.rect = self.image.get_rect()
        self.rect.width = SHOT_M_WIDTH
        self.rect.height = SHOT_M_HEIGHT
        self.rect.x = x
        self.rect.y = y + self.rect.height
        self.xpos = x + self.rect.width/2
        self.ypos = y + self.rect.height/2
        self.xvel = xvel
        self.yvel = yvel
        self.radius = self.rect.width/2
        self.friendly = alignment #0 friendly, 1 is enemy
        self.active = True
        
        self.enemy_m = pygame.image.load("gfx/Enemy_Small_Shot.png")
        self.enemy_l = pygame.image.load("gfx/Enemy_Large_Shot.png")
        
    def update(self):
        """update the shot's position"""
        
        if self.player_size == -1:
            self.image = self.image_m
            self.rect.width = SHOT_M_WIDTH
            self.rect.height = SHOT_M_HEIGHT
        elif self.player_size == 0:
            if self.friendly == 1:
                self.image = self.enemy_m
                self.rect.width = SHOT_M_WIDTH
                self.rect.height = SHOT_M_HEIGHT
            else:
                self.image = self.image_l
                self.rect.width = SHOT_L_WIDTH
                self.rect.height = SHOT_L_HEIGHT
        elif self.player_size == 1:
            if self.friendly == 1:
                self.image = self.enemy_l
            else:
                self.image = self.image_l
            self.rect.width = SHOT_L_WIDTH
            self.rect.height = SHOT_L_HEIGHT

        self.rect = self.rect.move(self.xvel, self.yvel)
        if self.rect.bottom < 0:#if above the screen
            self.active = False
        self.radius = self.rect.width/2
        self.xpos = self.rect.x + self.rect.width/2
        self.ypos = self.rect.y + self.rect.height/2
        
    def draw(self, screen):
        """draws the shot"""
        screen.blit(self.image, self.rect)
        
class Homing_Shot(Shot):
    def __init__(self, x, y, xvel, yvel, alignment, player):
        Shot.__init__(self, x, y, xvel, yvel, alignment, player)
        self.image = pygame.image.load("gfx/Homing_Shot.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.width = SHOT_E_WIDTH
        self.rect.height = SHOT_E_HEIGHT
        self.rect.x = x
        self.rect.y = y
        self.alignment = 1 # enemy shot
        self.frames = 0
        self.frames_list = HOMING_FRAMES_LIST
        self.f_delay = HOMING_FRAMES_DELAY
        self.timer = HOMING_TIMER
        
    def update(self):
        """update the shot's position"""
        # if the player is to the left of the mosquito
        if self.player.xpos < self.rect.x:
            tmpXVel = -self.xvel
        # if the player is to the right of the mosquito
        elif self.player.xpos > self.rect.x + self.rect.w:
            tmpXVel = self.xvel
        else:
            tmpXVel = 0
        # if the player is above the mosquito
        if self.player.ypos < self.rect.y: 
            tmpYVel = -self.yvel
        # if the player is below the mosquito        
        elif self.player.ypos > self.rect.y + self.rect.h:
            tmpYVel = self.yvel
        else:
            tmpYVel = 0


        future = self.rect.move(tmpXVel,tmpYVel)
        self.rect = future
        if self.rect.bottom < 0:#if above the screen
            self.active = False
        self.xpos = self.rect.x + self.rect.width/2
        self.ypos = self.rect.y + self.rect.height/2
        
    def draw(self, screen):
        screen.blit(self.image, self.rect, pygame.Rect(self.rect.width * self.frames_list[((self.frames/self.f_delay) % len(self.frames_list))], 0, self.rect.width, self.rect.height))
        self.advance_frame()
    def advance_frame(self):
        self.frames += 1
        if self.frames >= self.timer:
            self.active = False
        

        
